package limiter

import "gitee.com/DengAnbang/goutils/loge"

type ConnLimiter struct {
	concurrentConn int
	bucket         chan int
}

func NewConnLimiter(cc int) *ConnLimiter {
	return &ConnLimiter{
		concurrentConn: cc,
		bucket:         make(chan int, cc),
	}
}

func (cl ConnLimiter) GetConn() bool {
	if len(cl.bucket) >= cl.concurrentConn {
		return false
	}
	cl.bucket <- 1
	loge.E("获取令牌", len(cl.bucket))
	return true
}
func (cl ConnLimiter) ReleaseConn() {
	<-cl.bucket
	loge.E("释放令牌", len(cl.bucket))
}
